define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coursetable/coursetable/index',
                    add_url: 'coursetable/coursetable/add',
                    edit_url: 'coursetable/coursetable/edit',
                    multi_url: 'coursetable/coursetable/multi',
                    del_url: 'coursetable/coursetable/del',
                    table: 'fa_course_table',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                queryParams: function (params) {
                    params.rel_type = Config.relType;
                    params.rel_id = Config.relId;
                    return params;
                },
                escape: false,
                pk: 'id',
                sortName: 'weigh desc,id desc', //要使排序生效，在前面添加 weigh desc, 并且数据表weigh字段不能全为0,controller里$list=...->order前再添加->order("weigh desc")
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: '课程表名称'},
                        {field: 'school_id', title: '学校'},
                        {field: 'ocation_id', title: '地点'},
                        {field: 'classes_id', title: '班级'},
                        {field: 'teacher_id', title: '老师'},
                        {
                            field: 'start_date',
                            title: '项目开始时间',
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD"
                        },
                        {field: 'exclint_holidays', title: '排除国际假日'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'classplan',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-user-friends',
                                    click: function (value, row, index) {
                                        fileter = encodeURIComponent('{"table_id":"' + row.id + '","relType":"school"}');
                                        op = encodeURIComponent('{"table_id":"=","relType":"="}');
                                        Backend.api.open('/coursetable/classplant?relType=school&table_id=' + row.id + '&filter=' + fileter + '&op=' + op + '&ref=addtabs', '班级参数设置', {
                                            max: true,
                                            area: ['100%', '100%'],
                                            //offset: ['0px', '0px'],
                                            callback: function (data) {
                                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                            }
                                        });
                                    }
                                }, {
                                    name: 'tableplan',
                                    classname: 'btn btn-xs btn-warning btn-click',
                                    icon: 'fa fa-calendar-alt',
                                    click: function (value, row, index) {
                                        fileter = encodeURIComponent('{"table_id":"' + row.id + '","relType":"school"}');
                                        op = encodeURIComponent('{"table_id":"=","relType":"="}');
                                        Backend.api.open('/coursetable/autoplan?relType=school&table_id=' + row.id + '&filter=' + fileter + '&op=' + op + '&ref=addtabs', '排课', {
                                            max: true,
                                            area: ['100%', '100%'],
                                            //offset: ['0px', '0px'],
                                            callback: function (data) {
                                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                            }
                                        });
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js';
            document.head.appendChild(script);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            flatpickr('#c-exclint_day', {
                mode: 'multiple', // 设置为多选模式
                dateFormat: 'Y-m-d', // 设置日期格式
                enableTime: false, // 禁用时间选择
                defaultDate: '{$row.exclint_day}', // 设置默认日期
                locale: zh.zhCN,
                onChange: function (selectedDates, dateStr, instance) {
                    // 监听日期选择事件，手动更新输入框的值
                    $('#c-exclint_day').val(dateStr);
                }
            });
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        edit: function () {
            flatpickr('#c-exclint_day', {
                mode: 'multiple', // 设置为多选模式
                dateFormat: 'Y-m-d', // 设置日期格式
                enableTime: false, // 禁用时间选择
                defaultDate: '{$row.exclint_day}', // 设置默认日期
                locale: zh.zhCN,
                onChange: function (selectedDates, dateStr, instance) {
                    // 监听日期选择事件，手动更新输入框的值
                    $('#c-exclint_day').val(dateStr);
                }
            });
            var row = JSON.parse($('#row').val());
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="coursetable/coursetable/status" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status == 0 || row.status == null ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                changeStatus: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="coursetable/coursetable/coursetable/coursetable/changeStatus" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status === 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
            }
        }
    };
    return Controller;
});